package org.jeecg.modules.online.desform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.online.desform.constant.DesformConstant;
import org.jeecg.modules.online.desform.entity.DesignFormRoute;
import org.jeecg.modules.online.desform.mapper.DesignFormRouteMapper;
import org.jeecg.modules.online.desform.service.IDesignFormRouteService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 表单设计器路由跳转配置表
 *
 * @Author: jeecg-boot
 * @Date: 2020-03-25
 * @Version: V1.0
 */
@Service("designFormRouteServiceImpl")
public class DesignFormRouteServiceImpl extends ServiceImpl<DesignFormRouteMapper, DesignFormRoute> implements IDesignFormRouteService {

    @Override
    public DesignFormRoute getEffectiveRoute(String desformCode) {
        LambdaQueryWrapper<DesignFormRoute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DesignFormRoute::getDesformCode, desformCode);
        queryWrapper.eq(DesignFormRoute::getStatus, DesformConstant.ROUTE_STATUS_1);
        return super.getOne(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean editById(DesignFormRoute designFormRoute) {
        // 如果状态更改为启用，就将其他的设为禁用
        if (DesformConstant.ROUTE_STATUS_1.equals(designFormRoute.getStatus())) {
            DesignFormRoute updateEntity = new DesignFormRoute();
            updateEntity.setStatus(DesformConstant.ROUTE_STATUS_0);
            LambdaQueryWrapper<DesignFormRoute> updateQueryWrapper = new LambdaQueryWrapper<>();
            updateQueryWrapper.eq(DesignFormRoute::getDesformCode, designFormRoute.getDesformCode());
            updateQueryWrapper.eq(DesignFormRoute::getDesformId, designFormRoute.getDesformId());
            super.update(updateEntity, updateQueryWrapper);
        }
        return super.updateById(designFormRoute);
    }
}
